package com.xkdx.guangguang.adapter;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.card.CardpartDetail;
import com.xkdx.guangguang.fragment.brand.branch.BranchFragment;

public class CardDetailAdapter extends BaseAdapter {
	List<CardpartDetail> list;
	Context context;

	public CardDetailAdapter(Context context, List<CardpartDetail> list) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder viewHolder;
		if (view == null) {
			viewHolder = new ViewHolder();
			view = LayoutInflater.from(context).inflate(
					R.layout.home_detail_item, null);
			viewHolder.image = (ImageView) view
					.findViewById(R.id.home_detaile_item_address);
			viewHolder.name = (TextView) view
					.findViewById(R.id.home_detail_shoporbrandname);
			viewHolder.address = (TextView) view
					.findViewById(R.id.home_detail_tv_address);
			viewHolder.phone = (TextView) view
					.findViewById(R.id.home_detail_tv_phone);
			viewHolder.ll_home_detail_item = (LinearLayout) view
					.findViewById(R.id.ll_home_detail_item);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		viewHolder.name.setText(list.get(position).getShopName());
		viewHolder.address.setText(list.get(position).getAddress());
		// viewHolder.image.setImageResource(R.drawable.home_website);
		viewHolder.phone.setText(list.get(position).getPhone());
		viewHolder.ll_home_detail_item
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						FragmentTransaction ft = ((ManagerActivity) context)
								.getSupportFragmentManager().beginTransaction();

						BranchFragment shopInfoFragment = new BranchFragment();
						Bundle b1 = new Bundle();
						b1.putString("shopID", list.get(position).getShopID());
						shopInfoFragment.setArguments(b1);
						ft.replace(R.id.fragment, shopInfoFragment);
						ft.addToBackStack(null);
						ft.commit();

					}
				});

		return view;
	}
}
